package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApUnlikesBehaviorMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApUnlikesBehaviorService;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import jdk.nashorn.internal.objects.NativeNumber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Description:
 * @Version: V1.0
 */
@Service
@Transactional
public class ApUnlikesBehaviorServiceImpl extends ServiceImpl<ApUnlikesBehaviorMapper, ApUnlikesBehavior>
        implements ApUnlikesBehaviorService {

    /**
     * 保存不喜欢行为
     *
     * @param dto
     * @return
     */
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;

    @Override
    public ResponseResult unlike(UnLikesBehaviorDto dto) {
        //1.参数校验
        if (dto == null || dto.getArticleId() == null || dto.getType() < 0 || dto.getType() > 1 ){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //登录
        ApUser user = AppThreadLocalUtils.getUser();
        //判断是否登录
        if (user == null ){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN,"请先登录");
        }
        //2.查询行为实体 主键
        ApBehaviorEntry behaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        if (behaviorEntry == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //3.查询不喜欢行为是否存在
        ApUnlikesBehavior behavior = getOne(Wrappers.<ApUnlikesBehavior>lambdaQuery()
                .eq(ApUnlikesBehavior::getArticleId, dto.getArticleId())
                .eq(ApUnlikesBehavior::getEntryId, behaviorEntry.getId()));
        //4.不存在，判断是否是不喜欢行为
        if (behavior == null && dto.getType().equals(ApUnlikesBehavior.Type.CANCEL.getCode())){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"暂无不喜欢");
        }
        //5.已存在，判断重复不喜欢
        if (behavior != null && behavior.getType().equals(dto.getType())){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"请勿重复不喜欢");
        }
        //保存不喜欢
        if (behavior == null && dto.getType().equals(ApUnlikesBehavior.Type.UNLIKE.getCode())){
            behavior = new ApUnlikesBehavior();
            behavior.setArticleId(dto.getArticleId());
            behavior.setCreatedTime(new Date());
            behavior.setEntryId(behaviorEntry.getId());
            behavior.setType(dto.getType());

            save(behavior);
        } else {
            //或者取消不喜欢
            behavior.setType(dto.getType());
            behavior.setArticleId(dto.getArticleId());
            updateById(behavior);
        }
        //6.返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
